package panel.viewSpots.navigation
{
	import assists.Lang;
	import assists.SizeManager;
	import assists.URI;
	import assists.media.SoundMedia;
	import assists.resource.ResourceLoader;
	
	import com.greensock.TweenLite;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	
	import inerface.INavigation;
	
	import main.ClientConstants;
	
	import panel.viewSpots.StaticSlide;
	
	public class Navigation extends Sprite implements INavigation
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _lang:Lang;
		private var _uri:URI;
		private var _backGroundLayer:Sprite;
		private var _btnLayer:Sprite;
		private var _onOpenMusic:Function;
		
		private var _pathChoose:PathChoose;
		private var _pathItemList:Array = [];

		private var _spotGroup:NavigationSpotGroup;

		private var _backGround:Sprite;
		private var _spotChoose:ViewSpotChoose;

		private var backFunc:Function;
		private var _pathTime:uint;

		private var _btnsGroup:NavigationBtnGroup;
		private var _onOpenDetail:Function;
		private var _soundMedia:SoundMedia;
		private var _initScale:Point = new Point();
		public function Navigation()
		{
			super();
		}
		
		public  function init(stage:Stage,loader:ResourceLoader,lang:Lang,uri:URI = null):void
		{
			_stage = stage;
			_loader = loader;
			_lang = lang;
			_uri = uri;
			
			_backGroundLayer = new Sprite();
			_btnLayer = new Sprite();
			
			this.addChild(_backGroundLayer);
			this.addChild(_btnLayer);
			addListener();
			
			_pathChoose = new PathChoose();
			
			_btnsGroup = new NavigationBtnGroup(_loader,_stage,lang);
//			_btnsGroup.init(changeMapScale,backFunc);
			_btnLayer.addChild(_btnsGroup);
			
			if(ClientConstants.VIEWSPOT_SHOW)
			{
			_spotChoose = new ViewSpotChoose(_loader,_stage);
			_btnLayer.addChild(_spotChoose);
			ClientConstants.VIEWSPOT_SHOW = false;
			}
		}
		
		public function panelFunc(...args):void
		{
			backFunc = args[0];
		}
		
		public function set onSoundMedia(value:SoundMedia):void
		{
			if(_soundMedia) return;
			_soundMedia = value;
			_btnsGroup.onSoundMedia = value;
		}
		
		private function plusScale():void
		{
			trace("plus");
		}
		
		private function minScale():void
		{
			trace("min");
		}
		
		private function changeToMyPos():void
		{
		   	trace("changePos");
		}
		
		private function addListener():void
		{
			_backGroundLayer.addEventListener(MouseEvent.MOUSE_DOWN,touchBeginHandler);
		}
		
		protected function touchEndHandler(event:MouseEvent):void
		{
			_backGroundLayer.stopDrag();
			_backGroundLayer.removeEventListener(Event.ENTER_FRAME,frameHandler);
			_backGroundLayer.removeEventListener(MouseEvent.MOUSE_UP,touchEndHandler);
			isOverScreen();
		}
		
		protected function touchBeginHandler(event:MouseEvent):void
		{
			if(event.target == _backGround)
			{
			_spotGroup.hidePoaPao();
			_backGroundLayer.startDrag();
			_backGroundLayer.addEventListener(MouseEvent.MOUSE_UP,touchEndHandler);
			_backGroundLayer.addEventListener(Event.ENTER_FRAME,frameHandler);
			}
		}
		
		protected function frameHandler(event:Event):void
		{
			isOverScreen();
		}
		
		private function isOverScreen():void
		{
			if(_backGroundLayer.x > 0)
			{
				_backGroundLayer.x = 0;
			}else if(_backGroundLayer.x < _stage.stageWidth - _backGround.width)
			{
				_backGroundLayer.x = _stage.stageWidth - _backGround.width;
			}
			if(_backGroundLayer.y > 0)
			{
				_backGroundLayer.y = 0;
			}else if(_backGroundLayer.y < _stage.stageHeight - _backGround.height)
			{
				_backGroundLayer.y = _stage.stageHeight - _backGround.height;
			}
		}
		
		public function clear():void
		{
			
		}
		
		public function set onOpenMusic(value:Function):void
		{
			_onOpenMusic = value;
		}
		
		public function load(data:Object,key:String):void
		{
			var panelSpr:Sprite = _loader.getContent(_uri.getNavigationSwf("map",key));
			panelSpr = panelSpr.getChildAt(0) as Sprite;
			var backGroundInfo:Object = data.backGround;
			SizeManager.changeImageScale(panelSpr["backGround"]);
			_backGround = panelSpr["backGround"]
			_backGroundLayer.addChild(_backGround);
			
			var tempSpot:Object;
			for(var i:int=0;i<data.path.length;i++)
			{
				tempSpot = data.path[i];
				var pathItem:NavigationPath = new NavigationPath();
				pathItem.data = tempSpot;
				pathItem.y = pathItem.y + ClientConstants.HOME_UP_PER * _stage.stageHeight;
				pathItem.init(panelSpr["path" + (i + 1)]);
				_pathItemList.push({container:pathItem,info:tempSpot});
				_backGroundLayer.addChild(pathItem);
			}
			
			
			_spotGroup = new NavigationSpotGroup();
			_spotGroup.centerFunc = center;
			_spotGroup.onOpenDetail = _onOpenDetail;
			_spotGroup.init(data.viewSpot,_loader,_uri,panelSpr,_onOpenMusic,chagePath,_stage);
//			_spotGroup.y = _spotGroup.y + ClientConstants.HOME_UP_PER * _stage.stageHeight;
			_backGroundLayer.addChild(_spotGroup);			
			
			if(_spotChoose)
			{
			_spotChoose.show(data.viewSpot,startSeekPath);
			_pathChoose.init(data.path);
			}else
			{
				renderPath();
				_spotGroup.changePathArrow(_pathChoose.normalPath);
				updateUpInfo();
			}
			
		}
		
		private function renderPath():void
		{
			for(var i:int=0;i<_pathItemList.length;i++)
			{
				isInPath(_pathItemList[i].container,_pathItemList[i].info);
			}
		}
		
		private function startSeekPath(startIndex:String):void
		{
			_spotChoose.clear();
			_pathChoose.setStartIndex(startIndex);
			renderPath();
			_spotGroup.changePathArrow(_pathChoose.normalPath);
			updateUpInfo();
		}
		
		private function chagePath(isAdd:Boolean,spotIndex:String):void
		{
			_spotGroup.showCircleIco();
			var addList:Array;
			var removeList:Array;
			if(isAdd)
			{
				_pathChoose.addPathNode(spotIndex);
				addList = _pathChoose.addPath;
				removeList = _pathChoose.removePath;
			}else
			{
				if(!isNeedRemove(spotIndex)) return;
				_pathChoose.removeSpot(spotIndex);
				addList =  _pathChoose.addPath;
				removeList = _pathChoose.removePath;
			}
			for(i=0;i<removeList.length - 1;i++)
			{
				seekPath(removeList[i].index,removeList[i+1].index,false);
			}
			for(var i:int=0;i<addList.length - 1;i++)
			{
				seekPath(addList[i].index,addList[i+1].index,true);
			}
			
			_spotGroup.changePathArrow(_pathChoose.normalPath);
			updateUpInfo();
		}
		
		private function isNeedRemove(spotIndex:String):Boolean
		{
			var normalPath:Array = _pathChoose.normalPath;
			for(var i:int=0;i<normalPath.length;i++)
			{
				if(normalPath[i].index == spotIndex)
				{
					return true;
				}
			}
			return false;
		}
		
		private function seekPath(startIndex:String,endIndex:String,isAdd:Boolean):void
		{
			if(endIndex == null) return;
			if(startIndex == endIndex && isAdd)//如果是一个环线
			{
				_spotGroup.showCircleIco(startIndex);
				return;
			}
			var pathList:Array;
			for(var i:int=0;i<_pathItemList.length;i++)
			{
				pathList = _pathItemList[i].info.pathID.split("-");
				if((pathList[0] == startIndex && pathList[1] == endIndex) || (pathList[1] == startIndex && pathList[0] == endIndex))
				{
					changePathColor(_pathItemList[i].container,isAdd);
					break;
				}
			}
		}
		
		private function isInPath(path:NavigationPath,data:Object):void
		{
			var pathList:Array = data.pathID.split("-");
			var normal:Array = _pathChoose.normalPath;
			var inPath:Boolean;
			for(var i:int=0;i<normal.length - 1;i++)
			{
				if((normal[i].index == pathList[0] && normal[i + 1].index == pathList[1]) || (normal[i + 1].index == pathList[0] && normal[i].index == pathList[1]))
				{
					inPath = true;
					changePathColor(path,true);
					break;
				}
			}
		}
		
		private function changePathColor(path:NavigationPath,isAdd:Boolean):void
		{
			var color:ColorTransform = new ColorTransform();
			if(isAdd)
			{
				color.greenMultiplier = 1;
				color.redMultiplier = 0;
				color.blueMultiplier = 0;
				path.isWalked = true;
				_pathTime = _pathTime + path.data.time;
			}else
			{
				color.greenMultiplier = 1;
				color.redMultiplier = 1;
				color.blueMultiplier = 1;
				path.isWalked = false;
				_pathTime = _pathTime - path.data.time;
			}
			path.transform.colorTransform = color;
		}
		
		private function updateUpInfo():void
		{
			_btnsGroup.update("test",_pathTime + _spotGroup.allSpotTime);
		}
		
		private function center(offX:Number,offY:Number):void
		{
			offX = offX * _backGroundLayer.scaleX;
			offY = offY * _backGroundLayer.scaleY;
			var offx:Number = - (offX + _backGroundLayer.x) + _stage.stageWidth / 2;
			var offy:Number = - (offY + _backGroundLayer.y) + _stage.stageHeight / 2;
			if(_backGroundLayer.x + offx > 0)
			{
				offx = - _backGroundLayer.x;
			}else if(offx + _backGroundLayer.x < _stage.stageWidth - _backGroundLayer.width)
			{
				offx = _stage.stageWidth - _backGroundLayer.width - _backGroundLayer.x;
			}
			if(_backGroundLayer.y + offy > 0)
			{
				offy = - _backGroundLayer.y;
			}else if(offy + _backGroundLayer.y< _stage.stageHeight - _backGroundLayer.height)
			{
				offy = _stage.stageHeight - _backGroundLayer.height - _backGroundLayer.y;
			}
			TweenLite.to(_backGroundLayer,0.8,{x:_backGroundLayer.x + offx,y:_backGroundLayer.y + offy})
		}
		
		private function changeMapScale(type:int):void
		{
			if(type == 1)//放大
			{
				if(_backGroundLayer.scaleX < 2)
				{
				
				_backGroundLayer.scaleX = _backGroundLayer.scaleY = _backGroundLayer.scaleY  + 0.2;
				_backGroundLayer.x = _backGroundLayer.x - (_backGroundLayer.x + _stage.stageWidth * 0.5)* 0.2;
				_backGroundLayer.y = _backGroundLayer.y - (_backGroundLayer.y + _stage.stageHeight * 0.5)* 0.2;
				}else
				{
					trace("已经是最大尺寸了");
				}
			}else
			{
				if(_backGroundLayer.scaleY > 1)
				{
					_backGroundLayer.scaleX = _backGroundLayer.scaleY = _backGroundLayer.scaleY  - 0.2;
					if(_backGroundLayer.height <= _stage.stageHeight)
					{
						_backGroundLayer.scaleX = _backGroundLayer.scaleY = 1;
						_backGroundLayer.y = 0;
					}else
					{
					_backGroundLayer.y = _backGroundLayer.y + (-_backGroundLayer.y + _stage.stageHeight * 0.5)* 0.2;
					if(_backGroundLayer.y > 0) 	_backGroundLayer.y = 0;
					}
					if(_backGroundLayer.width <= _stage.stageWidth)
					{
						_backGroundLayer.scaleX = _backGroundLayer.scaleY = 1;
						_backGroundLayer.x = 0;
					}else
					{
					_backGroundLayer.x = _backGroundLayer.x + (-_backGroundLayer.x + _stage.stageWidth * 0.5)* 0.2;
					if(_backGroundLayer.x > 0) 	_backGroundLayer.x = 0;
					}
				}else
				{
					trace("已经是最小尺寸了");
				}
			}
		}
		
		public function set onOpenDetail(value:Function):void
		{
			_onOpenDetail = value;
		}
		
	}
}